package com.dc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dc.entity.Orders;
import com.dc.entity.Shop;
import com.dc.service.OrdersService;
import com.dc.service.impl.OrdersServiceImpl;
import com.dc.tools.DBTools;
import com.dc.tools.PageData;

/**
 * Servlet implementation class OrdersServlet
 */
@WebServlet("/OrdersServlet")
public class OrdersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrdersService ordersService = new OrdersServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		String op = "";
		// 接收op
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		// 根据op判断执行哪个方法
		if ("queryByPage".equals(op)) {
			doQueryByPage(request, response);
		}else if("shopqueryByPage".equals(op)) {
			doShopQuery(request, response);
		}
	}

	private void doShopQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		//接收keywords
		if(request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		String shopname = "";
		//接收shopname
		if(request.getParameter("shopname") != null) {
			shopname = request.getParameter("shopname");
		}
		System.out.println("shopname:"+shopname);
		//根据shopname查到shopid
		String sql = "select shopid from shop where shopname = ?";
		List<Shop> list =(List<Shop>) DBTools.exQuery(sql, Shop.class, shopname);
		Shop shop = list.get(0);
		System.out.println("shop:"+shop);
		int shopid = shop.getShopid();
		System.out.println("shopid:"+shopid);
		int page = 1;
		int pageSize = 10;
		//获取当前页和每页展示数据
		if(request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		PageData<Orders> pd = ordersService.selectOrdersByShopName(page, pageSize, shopid,keywords);
		//将搜索的关键字转发到JSP
		//request.setAttribute("keywords", keywords);
		//将pd传递到jsp
		request.setAttribute("pd", pd);
		//转发
		request.getRequestDispatcher("shop_ordermanager.jsp").forward(request, response);
	}
	
	private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		//接收keywords
		if(request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		System.out.println("********************");
		System.out.println(keywords);
		int page = 1;
		int pageSize = 10;
		//获取当前页和每页展示数据
		if(request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		//调用service中的显示方法
		PageData<Orders> pd = ordersService.selectOrdersByPage(page, pageSize, keywords);
		if (page > 1) {
			keywords = (String) request.getSession().getAttribute("keywords");
		}
		//将搜索的关键字转发到JSP
		request.getSession().setAttribute("keywords", keywords);
		//将pd传递到jsp
		request.setAttribute("pd", pd);
		//转发
		request.getRequestDispatcher("admin_ordersmanager.jsp").forward(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
